<?php
/**
 * User MyReservations list Controller
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_User_MyReservations_PayController extends RM_User_MyReservations_MainController
{
    protected $formName = 'MyReservations_Pay';

    function showAction()
    {
        try {
            $reservation = $this->getReservation($this->_getParam('reservationID'));
        } catch (RM_Exception $e) {
            $this->view->error = $e->getMessage();
            return;
        }

        $this->view->reservationID = $reservation->id;

        $config = new RM_Config();
        $this->view->currencySymbol = $config->getCurrencySymbol();

        list($this->view->reservationJson, $this->view->reservationDue) = $this->_getSummaryReservationInformation($reservation);
    }

    function successAction()
    {
        
    }

    function cancelAction()
    {

    }

    function payAction()
    {
        try {
            $reservation = $this->getReservation($this->_getParam('reservationID'));
        } catch (RM_Exception $e) {
            $this->view->error = $e->getMessage();
            return;
        }

        $value = $this->_getParam('value', 0);
        if ($value < 0) {
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'ValueShouldBePositive');
            return;
        }

        $totalPrice = $reservation->getTotalPrice();
        $totalPaid = $reservation->getTotalPaid();
        $totalDue = $totalPrice - $totalPaid;
        if ($value > $totalDue) {
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'ValueShouldBeLessOrEqualTotalDue');
            return;
        }

        $pluginName = $this->_getParam('payment');
        if ($pluginName == null) {
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'WrongPaymentSelection').': '.$pluginName;
            return;
        }

        $pluginModel = new RM_Plugins();
        $pluginRow = $pluginModel->fetchByName($pluginName);        
        if ($pluginRow == null) {
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'WrongPaymentSelection').': '.$pluginName;
            return;
        }

        if ($pluginModel->isEnabled($pluginRow) == false) {
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'WrongPaymentSelection').': '.$pluginName;
            return;
        }

        $plugin = $pluginModel->get($pluginName);
        if (is_a($plugin, 'RM_Payments_Plugin') == false){
            $this->view->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'WrongPaymentSelection').': '.$pluginName;
            return;
        }

        $module = new RM_Module_MyReservations();
        try {
            $module->pay($reservation, $plugin, $value);
        } catch (RM_Payments_Transaction_Exception $e) {        
            $this->error = $this->_translate->_('User.MyReservations.Pay.Errors', 'InternalError');
        }
    }
}